package com.example.martian.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.martian.mapper.IndexMapper;
import com.example.martian.utils.BigDecimalUtils;
import com.example.martian.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class IndexService {

    @Autowired
    private IndexMapper indexMapper;

    //币种展示
    public Object showCoin(){
        JSONObject jsonObject = null;
        JSONArray jsonArray = new JSONArray();
        List<Map<String, Object>> maps = indexMapper.checkExchange();
        for (Map<String,Object> map : maps){
            jsonObject = new JSONObject();
            jsonObject.put("coinId",map.get("cmc_id"));
            jsonObject.put("coin",map.get("cmc_coin"));
            jsonObject.put("service_charge",map.get("cmc_service_charge"));
            jsonArray.add(jsonObject);
        }
        return Utils.toJSON("访问成功",jsonArray.toString(),"0");
    }

    //汇率查询 + 闪兑展示
    @Transactional
    public Object showExchange(String symbol){
        String msg = "";
        String state = "";
        JSONObject jsonObject = null;
        JSONArray jsonArray = new JSONArray();
        String symbleUpperCase = symbol.toUpperCase();
        char[] symbleArrayCh = symbleUpperCase .toCharArray();
        Arrays.sort(symbleArrayCh);
        String sortedStr = new String(symbleArrayCh);
        List<Map<String, Object>> maps = indexMapper.showExchange();
        for(Map<String,Object> map : maps){
            jsonObject = new JSONObject();
            String coins = map.get("coins").toString();
            char[] symbolCoins = coins.toCharArray();
            Arrays.sort(symbolCoins);
            String sortedStrCoins = new String(symbolCoins);
            if (symbleUpperCase.equals(coins)){
                jsonObject.put("exchangeRate",map.get("cmc_exchangeRate"));
                jsonObject.put("coinId",map.get("cmc_id"));
                jsonObject.put("coin",map.get("coin"));
                jsonObject.put("minMoney",map.get("cmc_minMoney"));
                jsonObject.put("coins",map.get("coins"));
                jsonObject.put("logo",map.get("cmc_logo"));
                jsonObject.put("ervice_charge",map.get("cmc_service_charge"));
                jsonArray.add(jsonObject);
                msg = "访问成功";
                state = "0";
                return Utils.toJSON(msg,jsonArray.toString(),state);
            } else if(sortedStr.equals(sortedStrCoins)) {
                double exchangeRatec = BigDecimalUtils.div(1,Double.parseDouble(map.get("cmc_exchangeRate").toString()),6);
                String exchangeRate = new DecimalFormat("0.0000000").format(exchangeRatec);
                jsonObject.put("exchangeRate",String.valueOf(exchangeRate));
                jsonObject.put("coinId",map.get("cmc_id"));
                jsonObject.put("coin",map.get("coin"));
                jsonObject.put("minMoney",map.get("cmc_minMoney"));
                jsonObject.put("coins",map.get("coins"));
                jsonObject.put("ervice_charge",map.get("cmc_service_charge"));
                jsonArray.add(jsonObject);
                msg = "访问成功";
                state = "0";
                return Utils.toJSON(msg,jsonArray.toString(),state);
            } else {
                msg = "暂不支持该币种兑换";
                state = "1";
            }

        }
        return Utils.toJSON(msg,jsonArray.toString(),state);
    }

    //闪兑
    @Transactional
    public Object exchange(String exchangeRate,Integer jypassword,String address,String service_charge,
                           BigDecimal exchangeMoney,String coin,String getCoin) {
        String money = "";
        String charge_money = "";
        String lastMoney = "";
        String add = "";
        JSONArray jsonArray = new JSONArray();
        List<Map<String, Object>> mapList = indexMapper.checkMoneyByAddress(address);
        JSONObject object = null;
        if (!coin.equals(getCoin)) {
            if (exchangeMoney.compareTo(BigDecimal.ZERO) == -1) {
                return Utils.toJSON("非法数量", jsonArray.toString(), "1");
            }
            for (Map<String, Object> map : mapList) {
                object = new JSONObject();
                object.put("coin", map.get("cmc_coin"));
                object.put("money", map.get("cmc_money"));
                object.put("jypassword", map.get("cmc_jypassword"));
            }
            if (coin.equals("USDT")){
                coin = "ETH_USDT";
            }
            String moneyByCoin = indexMapper.selectMoneyByCoin(coin,address);
            String moneyByGetCoin = indexMapper.selectMoneyByCoin(getCoin,address);
            //判断金额是否足够
            if (!(BigDecimalUtils.compare(exchangeMoney.toString(), moneyByCoin))) {
                //计算闪兑金额 money是闪兑换算的金额
                money = BigDecimalUtils.mul(exchangeMoney.toString(), exchangeRate, 8);
                //计算手续费
                charge_money = BigDecimalUtils.mul(exchangeMoney.toString(), service_charge, 8);
                //余额修改 lastMoney是闪兑后的金额
                String sub = BigDecimalUtils.sub(moneyByCoin,exchangeMoney.toString(), 8);
                lastMoney = BigDecimalUtils.sub(sub, charge_money, 8);
                //闪兑后  后者的金额
                add = BigDecimalUtils.add(money, moneyByGetCoin, 8);
            } else {
                return Utils.toJSON("可用金额不足", null, "2");
            }
            if (!(jypassword.equals(object.getInteger("jypassword")))) {
                return Utils.toJSON("交易密码错误，请重新输入", null, "3");
            } else {
                indexMapper.updateCoinMoney(address, coin, lastMoney);
                indexMapper.updateGetCoinMoney(address, getCoin, add);
            }
        } else {
            return Utils.toJSON("不能兑换相同币种", Utils.toJSONmap(), "4");
        }
        return Utils.toJSON("闪兑成功", jsonArray.toString(), "0");
    }

    //轮播图
    public Object getBannerList(){
        String result = net.sf.json.JSONArray.fromObject(indexMapper.getBannerList(0)).toString();
        return Utils.toJSON("加载成功", result, "0");
    }


}
